<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>销售单</title>
    <% include("../layout/css.html"){} %>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card card-box-shadow">
    	<div class="layui-card-body">
	        <div class="layui-form toolbar">
	            <div class="layui-form-item">
	                <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input name="keywords" class="layui-input search_key" type="text" placeholder="销售单号,客户"/>
                        </div>
	                </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">规格</label>
                        <div class="layui-input-inline mr0">
                            <input id="specification" name="specification" class="layui-input search_key"
                                   type="text" placeholder="规格" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">销售日期：</label>
                        <div class="layui-input-inline mr0">
                            <input id="date1" name="date1" class="layui-input search_key"
                                   type="text" placeholder="开始时间" autocomplete="off"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">到</label>
                        <div class="layui-input-inline mr0">
                            <input id="date2" name="date2" class="layui-input search_key"
                                   type="text" placeholder="结束时间" autocomplete="off"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">收款：</label>
                        <div class="layui-input-inline mr0">
                            <select id="stateSearch" name="state" lay-filter="stateSearch" lay-verify="" lay-search="">
                                <option value="">请选择</option>
                                <% for(dictionarydata in stateList) { %>
                                <option value="${dictionarydata.dictdata_name}">${dictionarydata.dictdata_name}</option>
                                <% } %>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">开票：</label>
                        <div class="layui-input-inline mr0">
                            <select id="invoiceSearch" name="invoice" lay-filter="invoiceSearch" lay-verify="" lay-search="">
                                <option value="">请选择</option>
                                <% for(dictionarydata in invoiceList) { %>
                                <option value="${dictionarydata.dictdata_name}">${dictionarydata.dictdata_name}</option>
                                <% } %>
                            </select>
                        </div>
                    </div>
	                <div class="layui-inline">
	                    <button class="layui-btn icon-btn" lay-filter="searchSubmit" id="searchSubmit" lay-submit>
	                        <i class="layui-icon">&#xe615;</i>搜索
	                    </button>
	                    <% if(so.hasPermission("sell:add")){ %>
	                    <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
	                    <% } %>
	                </div>
	            </div>
	        </div>
        	<table class="layui-table" id="sellTable" lay-filter="sellTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <% if(so.hasPermission("sell:print")){ %>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="print">打印</a>
    <% } %>
	<% if(so.hasPermission("sell:update")){ %>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <% } %>
    <% if(so.hasPermission("sell:delete")){ %>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <% } %>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelSell">
    <form id="modelSellForm" lay-filter="modelSellForm" class="layui-form model-form">
        <input name="id" id="id" type="hidden"/>
        <input name="companyId" type="hidden"/>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md4">
                <label class="layui-form-label">销售单号</label>
                <div class="layui-input-block">
                   <input id="code" name="code"  type="text" class="layui-input"  autocomplete="off"
                   lay-verType="tips" lay-verify="" placeholder="请输入销售单号"/>
                </div>
            </div>
            <div class="layui-col-md4">
                <label class="layui-form-label">客户</label>
                <div class="layui-input-block">
                   <input id="supplier" name="supplier"  type="text" class="layui-input"  autocomplete="off"
                   lay-verType="tips" lay-verify="" placeholder="请输入客户"/>
                </div>
            </div>
            <div class="layui-col-md4">
                <label class="layui-form-label">销售日期</label>
                <div class="layui-input-block">
                   <input id="sellTime" name="sellTime"  type="text" class="layui-input"  autocomplete="off"
                   lay-verType="tips" lay-verify="" placeholder="请输入销售日期"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md4">
                <label class="layui-form-label">仓库</label>
                <div class="layui-input-block">
                   <input id="warehouseName" name="warehouseName"  type="text" class="layui-input"  autocomplete="off"
                   lay-verType="tips" lay-verify="" placeholder="请输入仓库"/>
                </div>
            </div>
            <div class="layui-col-md4">
                <label class="layui-form-label">总金额</label>
                <div class="layui-input-block">
                   <input id="total" name="total"  type="text" class="layui-input"  autocomplete="off"
                   lay-verType="tips" lay-verify="" placeholder="请输入总金额"/>
                </div>
            </div>
            <div class="layui-col-md4">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                   <input id="remark" name="remark"  type="text" class="layui-input"  autocomplete="off"
                   lay-verType="tips" lay-verify="" placeholder="请输入备注"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSellSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableState">
    {{#  if(d.state === "未收款"){ }}
    <input type="checkbox" lay-filter="ckState" value="{{d.id}}" lay-skin="switch"
           lay-text="未收款|已收款" {{d.state=='未收款'?'checked':''}}/>
    {{#  }else if(d.state === "已收款"){ }}
    已付款
    {{#  } else { }}
    <input type="checkbox" lay-filter="ckState" value="{{d.id}}" lay-skin="switch"
           lay-text="部分收款|已收款" {{d.state=='部分收款'?'checked':''}}/>
    {{#  } }}
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableInvoice">
    {{#  if(d.invoice === "未开票"){ }}
    <input type="checkbox" lay-filter="ckInvoice" value="{{d.id}}" lay-skin="switch"
           lay-text="未开票|已开票" {{d.invoice=='未开票'?'checked':''}}/>
    {{#  }if(d.invoice === "已开票"){ }}
    已开票
    {{#  } else { }}
    无
    {{#  } }}
</script>
<!-- js部分 -->
<% include("../layout/js.html"){} %>
<script type="text/javascript">
layui.use(['layer', 'form', 'formX', 'index','table', 'util', 'admin', 'zTree', 'tableX','laydate'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var util = layui.util;
    var admin = layui.admin;
    var tableX = layui.tableX;
    var formX = layui.formX;
    var index = layui.index;
    var laydate = layui.laydate;

    //日期时间选择器
    laydate.render({
        elem: '#date1'
        ,type: 'date'
    });
    laydate.render({
        elem: '#date2'
        ,type: 'date'
    });


    // 渲染表格
    var insTb = tableX.render({
        elem: '#sellTable'
        ,url: 'sell/list'
        ,page: true
        ,toolbar: true
        ,cellMinWidth: 60
        ,cols: [[
            {type: 'numbers'}
            ,{field: "code", minWidth: "60", title: "销售单号", sort: true, hide:false }
            ,{field: "supplier", minWidth: "60", title: "客户", sort: true, hide:false }
            ,{field: "sellTime", minWidth: "60", title: "销售日期", sort: true, hide:false }
            // ,{field: "warehouseName", minWidth: "60", title: "仓库", sort: true, hide:false }
            ,{field: "total", minWidth: "60", title: "总金额", sort: true, hide:false }
            ,{field: "remark", minWidth: "60", title: "备注", sort: true, hide:false }
            ,{field: "state", minWidth: "60", title: "收款",templet: '#tableState', sort: true, hide:false }
            ,{field: "invoice", minWidth: "60", title: "开票",templet: '#tableInvoice', sort: true, hide:false }
            ,{field: "createTime", minWidth: "60", title: "创建时间", sort: true, hide:true ,templet: function (d) {
                    return util.toDateString(d.createTime);
                }
            }
            ,{field: "createUser", minWidth: "60", title: "创建人", sort: true, hide:true }
            ,{field: "updateTime", minWidth: "60", title: "更新时间", sort: true, hide:true ,templet: function (d) {
                    return util.toDateString(d.createTime);
                }
            }
            ,{field: "updateUser", minWidth: "60", title: "更新人", sort: true, hide:true }
            ,{align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]]
        ,where:{"state":$("#stateSearch").val()}
    });

    // 添加
    $('#btnAdd').click(function () {
        showEditModel();
    });

    // 搜索
    form.on('submit(searchSubmit)', function (data) {
        insTb.reload({where: data.field, page: {curr: 1}});
    });

    form.on('select(stateSearch)', function(data){
        $("#searchSubmit").click();
    });
    form.on('select(invoiceSearch)', function(data){
        $("#searchSubmit").click();
    });
    $('.search_key').bind('keypress',function(event){
        if(event.keyCode == "13")
        {
            console.log(111)
            $("#searchSubmit").click();
        }
    });

    // 工具条点击事件
    table.on('tool(sellTable)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') { // 修改
            showEditModel(data);
        } else if (layEvent === 'del') { // 删除
            doDel(obj);
        } else if (layEvent === 'print') { // 删除
            print(obj);
        }
    });

    // 删除
    function doDel(obj) {
        layer.confirm('确定要删除“' + obj.data.code + '”销售单吗？', {
            shade: .1,
            skin: 'layui-layer-admin'
        }, function (i) {
            layer.close(i);
            layer.load(2);
            $.post('sell/delete', {
                ids: obj.data.id
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload();
                    top.productStorageTableReload();
                    //obj.del();
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
        });
    }
    // 打印
    function print(obj) {
        index.openTab({
            title: '销售单打印',
            url: '../sell/sell/print?id='+obj.data.id+'&sellId='+obj.data.id,
            end: function() {
                // table.reload('userTable');
            }
        });
    }

    // 显示编辑弹窗
    function showEditModel(mSell) {
        index.openTab({
            title: (mSell ? '修改' : '添加') + '销售单',
            url: 'sell/sellSon?id='+(mSell!=undefined ? mSell.id : ""),
            end: function() {
                // table.reload('userTable');
            }
        });
        /*admin.open({
            type: 1,
            maxmin:true,
            closeBtn:1,
            resize: true,
            area:["900px","500px"],
            title: (mSell ? '修改' : '添加') + '销售单',
            content: $('#modelSell').html(),
            success: function (layero, dIndex) {
                var url = mSell ? 'sell/update' : 'sell/add';
                form.val('modelSellForm', mSell);  // 回显数据
                // 表单提交事件
                form.on('submit(modelSellSubmit)', function (data) {
                    layer.load(2);
                    $.post(url, data.field, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            layer.close(dIndex);
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload();
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');
                    $("button[lay-filter='modelSellSubmit']").attr("disabled","disabled");
                    setTimeout(function(){$("button[lay-filter='modelSellSubmit']").removeAttr("disabled");}, 1000 );
                    return false;
                });
                laydate.render({
                    elem: '#sellTime'
                    ,type: 'datetime'
                });

            }
        });*/
    }
    top.sellTable = function(){
        insTb.reload();
    }
    form.on('switch(ckState)', function (obj) {
            layer.load(2);
            $.post('sell/updateState', {
                id: obj.elem.value,
                state: obj.elem.checked ? "未收款" : "已收款"
            }, function (res) {
                layer.closeAll('loading');
                insTb.reload();
<!--                if (res.code == 200) {-->
<!--                    layer.msg(res.msg, {icon: 1});-->
<!--                } else {-->
<!--                    layer.msg(res.msg, {icon: 2});-->
<!--                    $(obj.elem).prop('checked', !obj.elem.checked);-->
<!--                    form.render('checkbox');-->
<!--                }-->
            }, 'json');
        });
        form.on('switch(ckInvoice)', function (obj) {
            layer.load(2);
            $.post('sell/updateState', {
                id: obj.elem.value,
                invoice: obj.elem.checked ? "未开票" : "已开票"
            }, function (res) {
                layer.closeAll('loading');
                insTb.reload();
<!--                if (res.code == 200) {-->
<!--                    layer.msg(res.msg, {icon: 1});-->
<!--                } else {-->
<!--                    layer.msg(res.msg, {icon: 2});-->
<!--                    $(obj.elem).prop('checked', !obj.elem.checked);-->
<!--                    form.render('checkbox');-->
<!--                }-->
            }, 'json');
        });
});
</script>
</body>
</html>
